home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / Mouse / frmUI.cs next >
Encoding:
Text File  |  2004-09-27  |  10.8 KB  |  306 lines

  1. //-----------------------------------------------------------------------------
  2. // File: frmUI.cs
  3. //
  4. // Desc: The Mouse sample obtains and displays Mouse data.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. //-----------------------------------------------------------------------------
  8. using System;
  9. using System.Windows.Forms;
  10. using Microsoft.DirectX.DirectInput;
  11. using Microsoft.DirectX;
  12. using System.Threading;
  13.  
  14. namespace Mouse
  15. {    
  16.     public class frmUI : System.Windows.Forms.Form
  17.     {
  18.         Device applicationDevice = null;
  19.         Thread threadData = null;
  20.         AutoResetEvent eventFire = null;
  21.         delegate void UIDelegate();
  22.  
  23.         private System.Windows.Forms.Label label1;
  24.         private System.Windows.Forms.Label lblButton0;
  25.         private System.Windows.Forms.Label label2;
  26.         private System.Windows.Forms.Label lblButton1;
  27.         private System.Windows.Forms.Label label3;
  28.         private System.Windows.Forms.Label lblButton2;
  29.         private System.Windows.Forms.Label label4;
  30.         private System.Windows.Forms.ListBox lbStatus;    
  31.         private System.ComponentModel.Container components = null;
  32.  
  33.         public void MouseEvent()
  34.         {
  35.             // This is the mouse event thread.
  36.             // This thread will wait until a
  37.             // mouse event occurrs, and invoke
  38.             // the delegate on the main thread to
  39.             // grab the current mouse state and update
  40.             // the UI.
  41.             while(Created)                
  42.             {
  43.                 eventFire.WaitOne(-1, false);
  44.                 
  45.                 try
  46.                 {
  47.                     applicationDevice.Poll();
  48.                 }
  49.                 catch(InputException)
  50.                 {
  51.                     continue;
  52.                 }
  53.                 this.BeginInvoke(new UIDelegate(UpdateUI));
  54.             }
  55.         }
  56.  
  57.         
  58.         
  59.         
  60.         [MTAThread]
  61.         public static void Main()
  62.         {
  63.             Application.Run(new frmUI());
  64.         }
  65.  
  66.  
  67.  
  68.  
  69.         public frmUI()
  70.         {
  71.             // Required for Windows Form Designer support.
  72.             InitializeComponent();
  73.         }
  74.  
  75.         
  76.         
  77.         
  78.         /// <summary>
  79.         /// Clean up any resources being used.
  80.         /// </summary>
  81.         protected override void Dispose(bool disposing)
  82.         {
  83.             if (disposing)
  84.             {
  85.                 if (components != null) 
  86.                 {
  87.                     components.Dispose();
  88.                 }
  89.             }
  90.             base.Dispose(disposing);
  91.             
  92.             eventFire.Set();
  93.         }
  94.         #region Windows Form Designer generated code
  95.  
  96.         
  97.         
  98.         
  99.         /// <summary>
  100.         /// Required method for Designer support - do not modify
  101.         /// the contents of this method with the code editor.
  102.         /// </summary>
  103.         private void InitializeComponent()
  104.         {
  105.             this.lbStatus = new System.Windows.Forms.ListBox();
  106.             this.lblButton2 = new System.Windows.Forms.Label();
  107.             this.lblButton0 = new System.Windows.Forms.Label();
  108.             this.lblButton1 = new System.Windows.Forms.Label();
  109.             this.label4 = new System.Windows.Forms.Label();
  110.             this.label1 = new System.Windows.Forms.Label();
  111.             this.label2 = new System.Windows.Forms.Label();
  112.             this.label3 = new System.Windows.Forms.Label();
  113.             this.SuspendLayout();
  114.             // 
  115.             // lbStatus
  116.             // 
  117.             this.lbStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  118.             this.lbStatus.Items.AddRange(new object[] {
  119.                                                           "0,0,0"});
  120.             this.lbStatus.Location = new System.Drawing.Point(32, 200);
  121.             this.lbStatus.Name = "lbStatus";
  122.             this.lbStatus.Size = new System.Drawing.Size(96, 106);
  123.             this.lbStatus.TabIndex = 4;
  124.             // 
  125.             // lblButton2
  126.             // 
  127.             this.lblButton2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  128.             this.lblButton2.Location = new System.Drawing.Point(32, 144);
  129.             this.lblButton2.Name = "lblButton2";
  130.             this.lblButton2.Size = new System.Drawing.Size(96, 16);
  131.             this.lblButton2.TabIndex = 0;
  132.             this.lblButton2.Text = "Up";
  133.             this.lblButton2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  134.             // 
  135.             // lblButton0
  136.             // 
  137.             this.lblButton0.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  138.             this.lblButton0.Location = new System.Drawing.Point(32, 32);
  139.             this.lblButton0.Name = "lblButton0";
  140.             this.lblButton0.Size = new System.Drawing.Size(96, 16);
  141.             this.lblButton0.TabIndex = 0;
  142.             this.lblButton0.Text = "Up";
  143.             this.lblButton0.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  144.             // 
  145.             // lblButton1
  146.             // 
  147.             this.lblButton1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  148.             this.lblButton1.Location = new System.Drawing.Point(32, 88);
  149.             this.lblButton1.Name = "lblButton1";
  150.             this.lblButton1.Size = new System.Drawing.Size(96, 16);
  151.             this.lblButton1.TabIndex = 0;
  152.             this.lblButton1.Text = "Up";
  153.             this.lblButton1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  154.             // 
  155.             // label4
  156.             // 
  157.             this.label4.AutoSize = true;
  158.             this.label4.Location = new System.Drawing.Point(32, 184);
  159.             this.label4.Name = "label4";
  160.             this.label4.Size = new System.Drawing.Size(99, 13);
  161.             this.label4.TabIndex = 3;
  162.             this.label4.Text = "Coordinates (x,y,z)";
  163.             this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  164.             // 
  165.             // label1
  166.             // 
  167.             this.label1.AutoSize = true;
  168.             this.label1.Location = new System.Drawing.Point(56, 16);
  169.             this.label1.Name = "label1";
  170.             this.label1.Size = new System.Drawing.Size(43, 13);
  171.             this.label1.TabIndex = 1;
  172.             this.label1.Text = "Button0";
  173.             // 
  174.             // label2
  175.             // 
  176.             this.label2.AutoSize = true;
  177.             this.label2.Location = new System.Drawing.Point(56, 72);
  178.             this.label2.Name = "label2";
  179.             this.label2.Size = new System.Drawing.Size(43, 13);
  180.             this.label2.TabIndex = 1;
  181.             this.label2.Text = "Button1";
  182.             // 
  183.             // label3
  184.             // 
  185.             this.label3.AutoSize = true;
  186.             this.label3.Location = new System.Drawing.Point(56, 128);
  187.             this.label3.Name = "label3";
  188.             this.label3.Size = new System.Drawing.Size(43, 13);
  189.             this.label3.TabIndex = 1;
  190.             this.label3.Text = "Button2";
  191.             // 
  192.             // frmUI
  193.             // 
  194.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  195.             this.ClientSize = new System.Drawing.Size(162, 325);
  196.             this.ControlBox = false;
  197.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  198.                                                                           this.lbStatus,
  199.                                                                           this.label4,
  200.                                                                           this.label3,
  201.                                                                           this.label2,
  202.                                                                           this.label1,
  203.                                                                           this.lblButton2,
  204.                                                                           this.lblButton1,
  205.                                                                           this.lblButton0});
  206.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  207.             this.KeyPreview = true;
  208.             this.MaximizeBox = false;
  209.             this.Name = "frmUI";
  210.             this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  211.             this.Text = "Mouse (Escape exits)";
  212.             this.Load += new System.EventHandler(this.frmUI_Load);
  213.             this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.frmUI_KeyUp);
  214.             this.Activated += new System.EventHandler(this.frmUI_Activated);
  215.             this.ResumeLayout(false);
  216.  
  217.         }
  218.         #endregion
  219.  
  220.         
  221.         
  222.         
  223.         private void frmUI_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  224.         {
  225.             // If escape is pressed, user wants to exit.
  226.             if (e.KeyCode == Keys.Escape)
  227.                 Dispose();
  228.         }
  229.         
  230.         
  231.         
  232.         
  233.         public void UpdateUI()
  234.         {
  235.             MouseState mouseStateData = new MouseState();
  236.             
  237.             // Get the current state of the mouse device.
  238.             mouseStateData = applicationDevice.CurrentMouseState;
  239.             byte[] buttons = mouseStateData.GetMouseButtons();
  240.             
  241.             // Display some info about the device.
  242.             if (0 != buttons[0]) 
  243.                 lblButton0.Text="Down";
  244.             else
  245.                 lblButton0.Text="Up";
  246.             if (0 != buttons[1])
  247.                 lblButton1.Text="Down";
  248.             else
  249.                 lblButton1.Text="Up";
  250.             if (0 != buttons[2])
  251.                 lblButton2.Text="Down";    
  252.             else
  253.                 lblButton2.Text="Up";
  254.             if (0 != (mouseStateData.X | mouseStateData.Y | mouseStateData.Z))
  255.             {
  256.                 lbStatus.Items.Add(mouseStateData.X + ", " + mouseStateData.Y + ", " + mouseStateData.Z);
  257.                 if (lbStatus.Items.Count > 10) 
  258.                     lbStatus.Items.RemoveAt(1);
  259.                 lbStatus.SelectedIndex = lbStatus.Items.Count-1;
  260.             }
  261.         }
  262.  
  263.         
  264.         
  265.         
  266.         private void frmUI_Load(object sender, System.EventArgs e)
  267.         {
  268.             threadData = new Thread(new ThreadStart(this.MouseEvent));
  269.             threadData.Start();
  270.  
  271.             eventFire = new AutoResetEvent(false);
  272.             
  273.             // Create the device.
  274.             try
  275.             {
  276.                 applicationDevice = new Device(SystemGuid.Mouse);
  277.             }
  278.             catch(InputException)
  279.             {
  280.                 MessageBox.Show("Unable to create device. Sample will now exit.");
  281.                 Close();
  282.             }
  283.             // Set the cooperative level for the device.
  284.             applicationDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
  285.             // Set a notification event.    
  286.             applicationDevice.SetEventNotification(eventFire);
  287.             // Acquire the device.
  288.             try{ applicationDevice.Acquire(); }
  289.             catch{}                
  290.         }
  291.  
  292.         
  293.         
  294.         
  295.         private void frmUI_Activated(object sender, System.EventArgs e)
  296.         {
  297.             // Acquire the device whenever the application window is activated.
  298.             if (null != applicationDevice)
  299.             {
  300.                 try{applicationDevice.Acquire();}
  301.                 catch{}
  302.             }
  303.         }
  304.     }
  305. }
  306.